package cloud.example.eureka_client.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author zjm
 * @date 2018/5/10.
 */
@Configuration
@Slf4j
public class RedisConfig {

  @Value("${spring.redis.host}")
  private String redisRemoteHost;

  @Value("${spring.redis.port}")
  private String redisPort;

  @Value("${spring.redis.pool.max-active}")
  private String redisPoolMaxActive;

  @Value("${spring.redis.pool.max-idle}")
  private String redisPoolMaxIdle;

  @Value("${spring.redis.pool.min-idle}")
  private String redisPoolMinIdle;

  @Value("${spring.redis.pool.max-wait}")
  private String redisPoolMaxWait;

  @Value("${spring.redis.timeout}")
  private Integer redisTimeout;

  private JedisPoolConfig getJedisPoolConfig() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(Integer.parseInt(redisPoolMaxActive));
    jedisPoolConfig.setMaxIdle(Integer.parseInt(redisPoolMaxIdle));
    jedisPoolConfig.setMinIdle(Integer.parseInt(redisPoolMinIdle));
    jedisPoolConfig.setMaxWaitMillis(Long.parseLong(redisPoolMaxWait));
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPoolConfig.setTestOnReturn(true);
    jedisPoolConfig.setTestWhileIdle(true);
    return jedisPoolConfig;
  }

  @Bean
  public JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(getJedisPoolConfig());
    jedisConnectionFactory.setHostName(redisRemoteHost);
    return jedisConnectionFactory;

  }

  @Bean
  public JedisPool redisPoolFactory() {
    JedisPool jedisPool = new JedisPool(getJedisPoolConfig(), redisRemoteHost, Integer.valueOf(redisPort), redisTimeout, null);

    log.info("JedisPool注入成功！");
    log.info("redis地址：" + redisRemoteHost + ":" + redisPort);
    return jedisPool;
  }

  @Bean
  public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    if (null == redisConnectionFactory) {
      log.error("Redis Template Service is not available");
      return null;
    }
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(ObjectMapper.class));
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
  }


 /* @Bean
  public RedisConnectionFactory jedisConnectionFactory() {
    JedisPool jedisPool=new JedisPool();
      JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(Integer.parseInt(redisPoolMaxActive));
    jedisPoolConfig.setMaxIdle(Integer.parseInt(redisPoolMaxIdle));
    jedisPoolConfig.setMinIdle(Integer.parseInt(redisPoolMinIdle));
    jedisPoolConfig.setMaxWaitMillis(Long.parseLong(redisPoolMaxWait));
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPoolConfig.setTestOnReturn(true);
    jedisPoolConfig.setTestWhileIdle(true);
    RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
      .master("mymaster")
      .sentinel("127.0.0.1", 26379)
      .sentinel("127.0.0.1", 26380);
    return new JedisConnectionFactory(sentinelConfig);
  }*/


 /*@Bean
  public JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration configuration =
      new RedisStandaloneConfiguration(redisRemoteHost, 6379);
    return new JedisConnectionFactory(configuration);
  }*/
}
